package com.atguigu.juc.sync;

/**
 * @author yanglin
 * @create 2024-02-01 16:23 星期四
 * description:
 */
public class SynchronizedClearDemo {
    /**
     * 锁消除
     */
    
    
    static Object objectLock = new Object();
    
    public void m1(){
//        synchronized (objectLock) {
//            System.out.println("hello SynchronizedClearDemo");
//        }
        //会产生锁消除问题，JIT编译器会无视它  synchronized(o) 每次 new 出来的，不存在了（每个线程都会new 一把锁）
        Object o = new Object();
        synchronized (o){
            System.out.println("hello SynchronizedClearDemo \t" + o.hashCode() + "\t" + objectLock.hashCode());
        }
    }

    public static void main(String[] args) {
        SynchronizedClearDemo synchronizedClearDemo = new SynchronizedClearDemo();
        for (int i = 1; i <10; i++) {
            new Thread(() -> {
                synchronizedClearDemo.m1();
            }).start();
        }
    }
    
}
